package com.wzp.Action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wzp.PO.Paging;
import com.wzp.PO.comment;
import com.wzp.PO.loginRecord;
import com.wzp.PO.user;
import com.wzp.Service.StandardService;
import com.wzp.Util.IpProbe;


@Controller
@RequestMapping("/standard")
public class StandardAction {
	@Resource
	private HttpServletRequest request;
	
	@Resource
	private HttpSession session;
	
	@Resource
	private StandardService standardService;
	
	@RequestMapping(value="/queryRowsByTableName",  method = RequestMethod.POST)
	@ResponseBody
	public int  queryRowsByTableName(String param) throws Exception{
		return standardService.queryRowsByTableName(param);
	}
	
	@RequestMapping(value="/addloginRecord",  method = RequestMethod.POST)
	@ResponseBody
	public void  addloginRecord(loginRecord login) throws Exception{
		login.setIpPath(IpProbe.getClientIpAddr(request));
		login.setUid(0);
		standardService.addloginRecord(login);
	}
	
//	@RequestMapping(value="/queryComment",  method = RequestMethod.POST)
//	@ResponseBody
//	public List<comment>  queryComment(Paging param) throws Exception{
//		return standardService.queryComment(param);
//	}
	
	@RequestMapping(value="/queryComment",  method = RequestMethod.POST)
	@ResponseBody
	public Paging  queryComment(Paging param) throws Exception{
		return standardService.PagingComment(param);
	}
	
	@RequestMapping(value="/addComment",  method = RequestMethod.POST)
	@ResponseBody
	public String  addComment(comment param) throws Exception{
		Integer userid=(Integer)session.getAttribute("usid");
		
		if(userid!=null){
			param.setUserid(userid);
		}else{
			param.setUserid(0);
			String username="游客";
			param.setUsername(username);
		}
		
		standardService.addComment(param);
		 return "1";
	}
	
	/*@RequestMapping(value="/queryDepartment",  method = RequestMethod.POST)
	@ResponseBody
	public List<DepartmentPO>  queryDepartment(user param) throws Exception{
		return standardService.queryDepartment();
	}
	
	@RequestMapping(value="/queryDepartmentType",  method = RequestMethod.POST)
	@ResponseBody
	public List<DepartmentType> queryDepartmentType(user param) throws Exception{
		return standardService.queryDepartmentType();
	}*/
	
	@RequestMapping(value="/login",  method = RequestMethod.POST)
	@ResponseBody
	public int selectuser(user param) throws Exception{
		
		int flag=standardService.selectuser(param);
		if(flag==1){
			user usid=standardService.queryUser(param);
			session.setAttribute("usid",usid.getId());
			
			loginRecord login = new loginRecord();
			login.setIpPath(IpProbe.getClientIpAddr(request));
			login.setUid(usid.getId());
			standardService.addloginRecord(login);
		}
		return flag;
	}
	
	@RequestMapping(value="/adduser",  method = RequestMethod.POST)
	@ResponseBody
	public int adduser(user param) throws Exception{
		param.setIpPath(IpProbe.getClientIpAddr(request));
		return standardService.adduser(param);
	}
	
	@RequestMapping(value="/queryuserByname",  method = RequestMethod.POST)
	@ResponseBody
	public int queryuserByname(user param) throws Exception{
		int flag = standardService.queryUserByname(param);
		if(flag!=0){
			flag = 1;
		}
		return flag;
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder) throws Exception {
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
	}
	

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

}
